from threading import local
from typing import Any


class TestSession:
    """
    测试会话对象，当前测试运行期间，可使用该对象进行临时属性存储以及访问，这些属性仅在当前测试线程内生效
    使用场景:存储token、cookie等，跨方法使用
    """
    __test_thread_local = local()
    __test_thread_local.thread_local_map = {}

    @classmethod
    def set_attribute(cls, key: str, value: Any) -> None:
        cls.__test_thread_local.thread_local_map[key] = value

    @classmethod
    def get_attribute(cls, key: str) -> Any:
        try:
            return cls.__test_thread_local.thread_local_map[key]
        except Exception as e:
            print(e)
            return None
